package com.lyeducation.controllers.dto.system;

import com.lyeducation.controllers.dto.BaseStatusDto;
import com.lyeducation.entities.system.SysRoleEntity;
import lombok.Getter;
import lombok.Setter;

import java.util.HashSet;
import java.util.Objects;

@Getter
@Setter
public class SysRoleDto extends BaseStatusDto {
  private String parentId;
  private HashSet<SysRoleDto> childRoles;
  //  private Integer level;

  public SysRoleEntity createSysRoleEntity() {
    SysRoleEntity role = new SysRoleEntity();
    role.setName(this.getName());
    role.setParentId(this.getParentId());
    //    role.setLevel(this.getLevel());
    return role;
  }

  public static SysRoleDto createSysRoleDtoBySysRoleEntity(SysRoleEntity sysRoleEntity) {
    SysRoleDto sysRoleDto = new SysRoleDto();
    sysRoleDto.setId(sysRoleEntity.getId());
    sysRoleDto.setParentId(sysRoleEntity.getParentId());
    sysRoleDto.setName(sysRoleEntity.getName());
    if (sysRoleDto.getChildRoles() == null) {
      sysRoleDto.setChildRoles(new HashSet<SysRoleDto>());
    }
    if (sysRoleEntity.getChildren() != null && sysRoleEntity.getChildren().size() > 0) {
      for (SysRoleEntity sysRoleEntity1 : sysRoleEntity.getChildren()) {
        sysRoleDto.getChildRoles().add(createSysRoleDtoBySysRoleEntity(sysRoleEntity1));
      }
    }
    return sysRoleDto;
  }

  public String getParentId() {
    return parentId;
  }

  public void setParentId(String parentId) {
    this.parentId = parentId;
  }

  //  public Integer getLevel() {
  //    return level;
  //  }
  //
  //  public void setLevel(Integer level) {
  //    this.level = level;
  //  }
  //
  //  public JsonResult checkLevel() {
  //    if (this.level == null) {
  //      return JsonResult.putFail("级别不能为空");
  //    }
  //    if (this.level < 1) {
  //      return JsonResult.putFail("级别不能小于1");
  //    }
  //    return null;
  //  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (!(o instanceof SysRoleDto)) {
        return false;
    }
    if (!super.equals(o)) {
        return false;
    }
    SysRoleDto that = (SysRoleDto) o;
    return Objects.equals(parentId, that.parentId);
  }

  @Override
  public int hashCode() {

    return Objects.hash(super.hashCode(), parentId);
  }
}
